from xpinyin import Pinyin
from app import db
from collections import Counter 
import difflib
from app.models.nameList import PopularWord
import numpy as np
#print(p.get_pinyin(u"上海"))
# print(string_similar('lin','yin'))

#名字打分 是否有流行字判断 打分

"""
名字测试类：入参 人名
设计思路：
获取名字拼音 声母韵母 查声母韵母重复率
pronunciation() 用来测试名字音律
popularword() 用来测试名字流行程度

"""
class NameTest():
    def __init__(self,name_word,sex):
        self.name_word = name_word
        self.sex = sex
    def pronunciation(self):
        p = Pinyin()
        name_py = ''
        if not self.name_word == None:
            name_py = p.get_pinyin(self.name_word).split('-')
        ylv = {
            'sheng':[],
            'yun':[]
        }
        for n in name_py:
            if n[1] == 'h':
                ylv['sheng'].append(n[:2])
                ylv['yun'].append(n[2:])
            else:
                ylv['sheng'].append(n[:1])
                ylv['yun'].append(n[1:])
        pj_s = (self.name_test(ylv['sheng']) + self.name_test(ylv['yun'])) / 2
        score = 100
        score = score - score * pj_s
        return score
    #工具方法 测试重复率方法 入参 ‘马明瑞’
    def name_test(self,names_arr):
        score = []
        if len(names_arr) == 2:
            score.append(self.string_similar(names_arr[0],names_arr[1]))
        elif len(names_arr) == 3:
            score.append(self.string_similar(names_arr[0],names_arr[1]))
            score.append(self.string_similar(names_arr[1],names_arr[2]))
            score.append(self.string_similar(names_arr[0],names_arr[2]))
        elif len(names_arr) == 4:
            score.append(self.string_similar(names_arr[0],names_arr[1]))
            score.append(self.string_similar(names_arr[1],names_arr[2]))
            score.append(self.string_similar(names_arr[2],names_arr[3]))
        else:
            score = 40
        score_pj = int(np.mean(score)) 
        return score_pj
    @staticmethod
    def string_similar(s1, s2): #重复度
        return difflib.SequenceMatcher(None, s1, s2).quick_ratio()
    #查名字流行度方法
    def popularword(self):
        fraction_arr = []
        fraction = ''
        average = 0
        fraction = db.session.query(PopularWord.fraction).filter(PopularWord.word==self.name_word,PopularWord.sex==self.sex).first()
        print('打印11')
        print(fraction)
        if fraction == '' or fraction == None:
            for item in self.name_word:
                fraction = db.session.query(PopularWord.fraction).filter(PopularWord.word==item,PopularWord.sex==self.sex).first()
                if not fraction == None and not fraction == '':
                    fraction_arr.append(fraction)
            if fraction_arr == []:
                average = 60
            else:
                average = np.mean(fraction_arr)
        else:
            average = 100
        return average
    #综合调用方法 音律占40% 流行度占60%  
    def count_score(self):
        yl_name = self.pronunciation()
        lx_name = self.popularword()
        if lx_name == 'nan':
            lx_name = 60
        count_score =  yl_name * 0.4 + lx_name*0.6
        return count_score

# n = NameTest('谦',0)
# print(n.count_score())